package ic

import (
	"bytes"
	"fmt"
	"image"
	"image/jpeg"
)

type JPEGConverter struct{}

func (j *JPEGConverter) SupportedFormat() string {
	return "JPEG"
}

func (j *JPEGConverter) FileExtension() string {
	return ".jpg"
}

func (j *JPEGConverter) MimeType() string {
	return "image/jpeg"
}

func (j *JPEGConverter) Encode(img image.Image, options ...Options) ([]byte, error) {
	var buf bytes.Buffer

	// 合并选项
	opts := MergeOptions(options...)

	jpegOptions := &jpeg.Options{
		Quality: opts.Quality,
	}

	if err := jpeg.Encode(&buf, img, jpegOptions); err != nil {
		return nil, fmt.Errorf("JPEG编码失败: %v", err)
	}

	return buf.Bytes(), nil
}

func (j *JPEGConverter) Decode(data []byte) (image.Image, error) {
	return jpeg.Decode(bytes.NewReader(data))
}
